package com.shenmazong.servertalkadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctc.wstx.util.StringUtil;
import com.shenmazong.pojo.TbClass;
import com.shenmazong.pojo.TbClassChangeRecord;
import com.shenmazong.pojo.TbClassTime;
import com.shenmazong.pojo.TbSubject;
import com.shenmazong.servertalkadmin.mapper.TbClassChangeRecordMapper;
import com.shenmazong.servertalkadmin.mapper.TbClassMapper;
import com.shenmazong.servertalkadmin.mapper.TbClassTimeMapper;
import com.shenmazong.servertalkadmin.mapper.TbSubjectMapper;
import com.shenmazong.servertalkadmin.service.TbClassChangeRecordService;
import com.shenmazong.servertalkadmin.service.TbClassService;
import com.shenmazong.utils.DateTime;
import com.shenmazong.utils.ResponseResult;
import com.shenmazong.vo.StudentVo;
import com.shenmazong.vo.TbClassVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class TbClassServiceImpl implements TbClassService {

    @Autowired
    private TbClassMapper tbClassMapper;

    @Autowired
    private TbClassTimeMapper tbClassTimeMapper;
    @Autowired
    private TbClassChangeRecordMapper tbClassChangeRecordMapper;
    @Autowired
    private TbClassChangeRecordService recordService;

    /*
     * @Description: 添加课程
     * @Author: 宋合
     * @Date: 2020/9/18 9:57
     * @param tbClass:
     * @return: com.shenmazong.utils.ResponseResult
     **/
    @Override
    public ResponseResult makeClass(TbClass tbClass) {

        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>(100);
        //获取传入的时间
        TbClassTime tbClassTime = tbClassTimeMapper.selectById(tbClass.getClasstimeId());
        if(tbClassTime==null){
            result.setFailMessage("时间段错误");
            return result;
        }
        //获取当前日期
        Date date = DateTime.getDate();
        //获取当前时间
        Date time = DateTime.getTime();
        //格式化日期
        Date classDate = DateTime.formatDate(tbClass.getClassDate());
        //格式化时间
        Date classTime =DateTime.formatTime(tbClassTime.getBeginTime());
        //判断是否小于当前日期
        if(classDate.getTime()<date.getTime()){
            result.setFailMessage("日期错误");
            return result;

        }


        //日期相同则比对时间是否小于当前时间
        if(classDate.getTime()==date.getTime()){
            if(classTime.getTime()<time.getTime()){
                result.setFailMessage("时间错误");
                return result;
            }
        }
        //判读老师该时间内是否有课
        List<TbClass> tbClassesTeacher = tbClassMapper.selectList(
                new QueryWrapper<TbClass>()
                    .eq("teacher_id", tbClass.getTeacherId())
                    .eq("classtime_id", tbClass.getClasstimeId())
                    .ne("class_status",3)
                    .eq("class_date",tbClass.getClassDate()));
        if(tbClassesTeacher.size()>0){
            result.setFailMessage("老师已有课程，无法添加");
            return result;
        }
        //判读学生该时间内是否有课
        List<TbClass> tbClassesStudent = tbClassMapper.selectList(
                new QueryWrapper<TbClass>()
                        .eq("student_id", tbClass.getStudentId())
                        .eq("classtime_id", tbClass.getClasstimeId())
                        .ne("class_status",3)
                        .eq("class_date",tbClass.getClassDate()));
        if(tbClassesStudent.size()>0){
            result.setFailMessage("学生已有课程，无法添加");
            return result;
        }
        //添加
        tbClassMapper.insert(tbClass);
        if(tbClass.getId()==null){
            result.setFailMessage("添加失败");
            return result;
        }
        resultMap.put("class",tbClass);
        result.setResult(resultMap);
        return result;
    }
    /*
     * @Description: 调课
     * @Author: 宋合
     * @Date: 2020/9/18 9:57
     * @param tbClass:
     * @return: com.shenmazong.utils.ResponseResult
     **/
    @Override
    public ResponseResult changeClass(TbClass tbClass) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>(100);

        //--1 查询课程是否存在
        TbClass tbClassNew = tbClassMapper.selectOne(
                new QueryWrapper<TbClass>()
                        .eq("id", tbClass.getId())
                        .ne("class_status", 3)
                        .last("limit 1")
        );

        //判断课程是否被删除或不存在
        if(tbClassNew == null) {
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("课程不存在");
            return result;
        }
        //已上完的课不能调课
        if(tbClassNew.getClassStatus()==1){
            result.setFailMessage("课程已上，无法调课");
            return result;
        }
        //查询传入的时间
        TbClassTime tbClassTime = tbClassTimeMapper.selectById(tbClass.getClasstimeId());
        if(tbClassTime==null){
            result.setFailMessage("时间段错误");
            return result;
        }
        //获取当前时间和日期
        Date date = DateTime.getDate();
        Date time = DateTime.getTime();
        //格式化日期
        Date classDate = DateTime.formatDate(tbClass.getClassDate());
        //格式化时间
        Date classTime =DateTime.formatTime(tbClassTime.getBeginTime());
        //判断是否小于当前日期
        if(classDate.getTime()<date.getTime()){
            result.setFailMessage("日期错误");
            return result;

        }

        if(classDate.getTime()==date.getTime()){
            //判断是否小于当前时间
            if(classTime.getTime()<time.getTime()){
                result.setFailMessage("时间错误");
                return result;
            }
        }
        //查询老师是否有课
        List<TbClass> tbClassesTeacher = tbClassMapper.selectList(
                new QueryWrapper<TbClass>()
                        .eq("teacher_id", tbClass.getTeacherId())
                        .eq("classtime_id", tbClass.getClasstimeId())
                        .eq("class_date",tbClass.getClassDate())
                        .eq("class_status", 0));
        if(tbClassesTeacher.size()>0){
            result.setFailMessage("老师已有课程，无法更改");
            return result;
        }
        //查询学生是否有课
        List<TbClass> tbClasseStudent = tbClassMapper.selectList(
                new QueryWrapper<TbClass>()
                        .eq("student_id", tbClass.getStudentId())
                        .eq("classtime_id", tbClass.getClasstimeId())
                        .eq("class_date",tbClass.getClassDate())
                        .eq("class_status", 0));
        if(tbClasseStudent.size()>0){
            result.setFailMessage("学生已有课程，无法更改");
            return result;
        }

        //-- 添加调课记录
        TbClassChangeRecord tbClassChangeRecord = new TbClassChangeRecord();
        tbClassChangeRecord.setClassId(tbClass.getId());
        tbClassChangeRecord.setNewClassTime(tbClass.getClasstimeId());
        tbClassChangeRecord.setOldClassTime(tbClassNew.getClasstimeId());
        tbClassChangeRecord.setStatus(0);
        tbClassChangeRecord.setStudentId(tbClass.getStudentId());
        tbClassChangeRecord.setTeacherId(tbClass.getTeacherId());
        tbClassChangeRecordMapper.insert(tbClassChangeRecord);

        //-- 修改原课时为调课状态
        tbClassNew.setClassStatus(3);
        tbClassMapper.updateById(tbClassNew);

        //-- 增加新课时
        tbClass.setClasstimeId(tbClassChangeRecord.getNewClassTime());
        tbClass.setId(null);
        tbClassMapper.insert(tbClass);
        if(tbClass.getId() == null) {
            result.setFailMessage("调课失败");
            return result;
        }

        resultMap.put("tbclass",tbClass);
        result.setResult(resultMap);

        return result;

    }
    /*
     * @Description: 删除课程
     * @Author: 宋合
     * @Date: 2020/9/18 9:57
     * @param tbClass:
     * @return: com.shenmazong.utils.ResponseResult
     **/
    @Override
    public ResponseResult closeClass(Integer id) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>(100);

        //--1 查询科目是否存在
        TbClass tbClass = tbClassMapper.selectById(id);

        int i=3;
        if(tbClass == null || tbClass.getClassStatus().equals(i)) {
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("课程不存在");
            return result;
        }
        tbClass.setClassStatus(3);
        tbClassMapper.updateById(tbClass);
        resultMap.put("tbclass",tbClass);
        result.setResult(resultMap);
        return result;
    }
    /*
     * @Description: 查询所有课程
     * @Author: 宋合
     * @Date: 2020/9/18 20:11
     * @param null:
     * @return: null
     **/


    @Override
    public ResponseResult classList(Integer pageCurrent,Integer pageSize){
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>(100);
        Page<StudentVo> page = new Page<StudentVo>(pageCurrent, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        //排斥已删除的
        queryWrapper.ne("class_status",3);
        IPage<TbClassVo> iPage = tbClassMapper.classList(page,queryWrapper);
        System.out.println(iPage.getRecords());
        if(iPage.getRecords().size()<=0){
            result.setFailMessage("查询失败");
            return result;
        }
        resultMap.put("classPage",iPage);
        result.setResult(resultMap);

        return result;
    }

}
